<?php

namespace Biz\service\activity;

use Biz\model\activity\ActivityPage as mActivityPage;
use Biz\model\activity\ActivityPageTempl as mActivityPageTempl;
use Biz\model\activity\PageModule as mPageModule;
use Encore\Admin\Facades\Admin;

class ActivityPage
{

    public static function pageModuleToolbar($grid, $toolsConfig)
    {
        $tools = '';
        foreach ($toolsConfig as $v) {
            switch ($v['type']) {
                case mPageModule::MODULE_TYPE_TAB:
                    $buttonId = "button-" . mt_rand();
                    $jump = app('request')->fullUrlWithQuery(['_timer' => 1]);
                    self::addModuleToolsScript($buttonId, $href = $grid->resource() . $v['href'], $jump);
                    $tools .= "<li><a href='javascript:void(0);' class='$buttonId'>" . mPageModule::getModuleTypeName($v['type']) . "</a></li>";
                    break;
                default:
                    if ($v['link'] == 'line') {
                        $tools .= '<li role="separator" class="divider"></li>';
                        continue;
                    }
                    $href = $grid->resource() . '/add/' . $v['type'];
                    $tools .= "<li><a href='$href'>" . mPageModule::getModuleTypeName($v['type']) . "</a></li>";
                    break;
            }
        }

        return <<<TOOLS
<div class="btn-group pull-right" style="margin-right: 10px">
    <a class="btn btn-sm btn-twitter" title="添加"><i class="fa fa-plus"></i><span class="hidden-xs"> 添加</span></a>
    <button type="button" class="btn btn-sm btn-twitter dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
        <span class="caret"></span>
        <span class="sr-only">Toggle Dropdown</span>
    </button>
    <ul id="menu3" class="dropdown-menu" aria-labelledby="drop6">
      $tools
    </ul>
</div>
TOOLS;
    }

    public static function addModuleToolsScript($buttonId, $href, $url)
    {
        $script = <<<EOT
(function () {
	$('.{$buttonId}').on('click', function () {
	     $.post("{$href}");
		setTimeout(function () {
			$.pjax({
				container: '#pjax-container',
				url: "{$url}"
			});
			toastr.success('操作成功', null, {
				timeOut: 2000
			});
		}, 2000);
	});
})();
EOT;
        Admin::script($script);
    }

    public static function admCreateOrEdit($basicData, $templData, $scene = 'create')
    {

        $result = mActivityPage::action(function () use ($basicData, $templData, $scene) {
            if ($scene == 'edit') {
                //编辑
                $activityId = $basicData['activity_id'];
                $result = mActivityPage::upData($activityId, $basicData);

                //删除已有的模板
                $ret = mActivityPageTempl::delListByActivityId($activityId);
                if ($ret == false) {
                    throw new \Exception('更新失败');
                }
            }

            if ($scene == 'create') {
                //添加
                $activityId = mActivityPage::create($basicData);

                if ($activityId == false) {
                    throw new \Exception('添加活动失败');
                }
            }

            if ($templData) {
                $templData = array_map(function ($v) use ($activityId) {
                    $queryData = [
                        'activity_id'      => $activityId,
                        'is_goods'         => isset($v['is_goods']) ? $v['is_goods'] : 0,
                        'type'             => isset($v['type']) ? $v['type'] : 0,
                        'goods_id'         => isset($v['goods_id']) ? $v['goods_id'] : 0,
                        'goods_id_first'   => isset($v['goods_id_first']) ? $v['goods_id_first'] : 0,
                        'goods_id_second'  => isset($v['goods_id_second']) ? $v['goods_id_second'] : 0,
                        'goods_sku'        => isset($v['goods_sku']) ? $v['goods_sku'] : 0,
                        'goods_sku_first'  => isset($v['goods_sku_first']) ? $v['goods_sku_first'] : 0,
                        'goods_sku_second' => isset($v['goods_sku_second']) ? $v['goods_sku_second'] : 0,
                        'un_price'         => isset($v['un_price']) ? $v['un_price'] : '0.00',
                        'un_price_first'   => isset($v['un_price_first']) ? $v['un_price_first'] : '0.00',
                        'un_price_second'  => isset($v['un_price_second']) ? $v['un_price_second'] : '0.00',
                        'banner'           => isset($v['banner']) ? $v['banner'] : '',
                        'link_type'        => isset($v['link_type']) ? $v['link_type'] : '0',
                        'link_target'      => isset($v['link_target']) ? $v['link_target'] : '0',
                        'link_url'         => isset($v['link_url']) ? $v['link_url'] : '',
                        'coupon_code'      => isset($v['coupon_code']) ? $v['coupon_code'] : '',
                        'sort'             => isset($v['sort']) ? $v['sort'] : '',
                        'status'           => isset($v['status']) ? $v['status'] : 1,
                        'addtime'          => date('Y-m-d H:i:s'),
                    ];
                    return $queryData;
                }, $templData);

                $rows = mActivityPageTempl::create($templData);

                if ($rows == false) {
                    if ($scene == 'crate') {
                        throw new \Exception('添加活动模板失败');
                    }
                    if ($scene == 'edit') {
                        throw new \Exception('更新活动模板失败');
                    }
                }
            }

            return true;
        });

        return $result;

    }

    public static function admGetList($where, $limit)
    {
        $list = mActivityPage::getList($where, $limit);
        return $list;
    }

    public static function admGetInfo($activityId)
    {
        $info = mActivityPage::getInfo($activityId);
        return $info;
    }

}